package co.fitstart.entity.principal;

import java.beans.Transient;
import java.util.Date;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicUpdate;

import com.demo2do.core.format.PersistentFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import co.fitstart.entity.utils.SignatureUtils;

/**
 * @author mohanlan
 */
@Entity
@Table(name = "principal")
@DynamicUpdate
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@PersistentFormat
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class Principal {
    
    @Id
    @GeneratedValue
    private Long id;
    
    private String mobile;
    
    private String name;
    
    private String password;
    
    private String authority;
    
    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name = "avatar", column = @Column(name = "avatar")),
        @AttributeOverride(name = "realname", column = @Column(name = "realname")),
        @AttributeOverride(name = "nickname", column = @Column(name = "nickname")),
        @AttributeOverride(name = "gender", column = @Column(name = "gender")),
        @AttributeOverride(name = "specialty", column = @Column(name = "specialty")),
        @AttributeOverride(name = "description", column = @Column(name = "description"))
    })
    private Profile profile;
    
    /** 财务系统编号 */
    private String financeCode;
    
    /** 员工类型 */
    @Enumerated(value = EnumType.STRING)
    private PrincipalType type;
    
    /** 标志是否是后台账号，主要用于登录时判断是否可以登录后台 */
    @Column(name = "is_admin")
    private boolean admin;
    
    /** 标志是否是企业号账号，主要用于登录时判断是否可以登录企业号 */
    @Column(name = "is_enterprise")
    private boolean enterprise;
    
    /** 上次修改密码时间 */
    private Date latestUpdateTime;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public Principal() {
        
    }
    
    public Principal(String mobile, String name) {
        this.mobile = mobile;
        this.name = name;
    }
    
    /**
     * Initialize principal basic fields
     * 
     * @param type
     * @return
     */
    public Principal initialize(PrincipalType type) {
        
        if(StringUtils.isNotBlank(this.mobile)) {
            // using mobile's last 6 digitals as password 
            this.password = DigestUtils.md5Hex(StringUtils.substring(this.mobile, this.mobile.length() - 6));
        }
        
        this.type = type;
        
        return this;
    }
    
    /**
     * 
     * @return
     */
    public Principal initializeAreaManager() {
        this.authority = "ROLE_AREA_MANAGER";
        this.type = PrincipalType.SITE;
        this.admin = true;
        this.enterprise = false;
        this.disabled = false;
        
        return this;
    }
    
    /**
     * 
     * @return
     */
    public Principal initializeAreaServer() {
        this.authority = "ROLE_AREA_SERVER";
        this.type = PrincipalType.SITE;
        this.admin = true;
        this.enterprise = false;
        this.disabled = false;
        
        return this;
    }
    /**
     * 
     * @return
     */
    public Principal initializeSiteManager() {
        this.authority = "ROLE_SITE_MANAGER";
        this.type = PrincipalType.SITE;
        this.admin = true;
        this.enterprise = false;
        this.disabled = false;
        
        return this;
    }
    
    /**
     * 
     * @param id
     * @return
     */
    public Principal initializeId(Long id) {
        this.id = id;
        return this;
    }
    
    /**
     * Change password according to input
     * 
     * @param password
     * @return
     */
    public Principal changePassword(String password) {
        this.password = SignatureUtils.makeMD5Signature(password);
        this.latestUpdateTime = new  Date();
        return this;
    }
    
    /**
     * Change password according to input
     * 
     * @return
     */
    public Principal resetPassword() {
        
        // using mobile's last 6 digitals as default password
        String finalString = StringUtils.substring(this.mobile, this.mobile.length() - 6);
        this.password = DigestUtils.md5Hex(finalString);
        this.latestUpdateTime = new Date();
        return this;
    }
    
    /**
     * 
     * @param profile
     * @return
     */
    public Principal editProfile(Profile profile) {
        this.profile  = profile;
        return this;
    }
    
    /**
     * 
     * @param avatar
     * @return
     */
    public Principal editAvatar(String avatar) {
        if(this.profile == null) {
            this.profile = new Profile();
        }
        this.profile.setAvatar(avatar);
        return this;
    }
    
    /**
     * 
     * @return
     */
    public Principal changeStatus() {
        this.disabled = this.disabled ? false : true;
        return this;
    }
    
    /**
     * 
     * @return
     */
    public Principal disabled() {
        this.disabled = true;
        return this;
    }
    
    public Principal edit(String mobile) {
        this.mobile = mobile;
        this.disabled = false;
        
        return this;
    }
    
    /**
     * 
     * @return
     */
    public boolean hasSite() {
        return !this.authority.equals("ROLE_ROOT") && !this.authority.equals("ROLE_ADMIN")
                && !this.authority.equals("ROLE_AREA_MANAGER") && !this.authority.equals("ROLE_AREA_SERVER");
    }
    
    /**
     * 
     * @return
     */
    public boolean isAreaManager() {
        return "ROLE_AREA_MANAGER".equals(this.authority);
    }
    
    /**
     * 
     * @return
     */
    public boolean isAreaServer() {
        return "ROLE_AREA_SERVER".equals(this.authority);
    }
    
    /**
     * 
     * @return
     */
    public boolean isSiteManager() {
        return this.authority.equals("ROLE_SITE_MANAGER");
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public boolean isSuper() {
        return !this.disabled && StringUtils.equalsIgnoreCase(this.authority, "ROLE_ADMIN");
    }
    /**
     * 
     * @return
     */
    @Transient
    public boolean isCoach() {
        return this.authority.equals("ROLE_COACH");
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public boolean isSiteServer() {
        return this.authority.equals("ROLE_SITE_SERVER");
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public String getIdString() {
        return this.id.toString();
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the mobile
     */
    public String getMobile() {
        return mobile;
    }

    /**
     * @param mobile the mobile to set
     */
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the authority
     */
    public String getAuthority() {
        return authority;
    }

    /**
     * @param authority the authority to set
     */
    public void setAuthority(String authority) {
        this.authority = authority;
    }

    /**
     * @return the profile
     */
    public Profile getProfile() {
        return profile;
    }

    /**
     * @param profile the profile to set
     */
    public void setProfile(Profile profile) {
        this.profile = profile;
    }

    /**
     * @return the financeCode
     */
    public String getFinanceCode() {
        return financeCode;
    }

    /**
     * @param financeCode the financeCode to set
     */
    public void setFinanceCode(String financeCode) {
        this.financeCode = financeCode;
    }

    /**
     * @return the type
     */
    public PrincipalType getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(PrincipalType type) {
        this.type = type;
    }

    /**
     * @return the admin
     */
    public boolean isAdmin() {
        return admin;
    }

    /**
     * @param admin the admin to set
     */
    public void setAdmin(boolean admin) {
        this.admin = admin;
    }

    /**
     * @return the enterprise
     */
    public boolean isEnterprise() {
        return enterprise;
    }

    /**
     * @param enterprise the enterprise to set
     */
    public void setEnterprise(boolean enterprise) {
        this.enterprise = enterprise;
    }

    /**
     * @return the latestUpdateTime
     */
    public Date getLatestUpdateTime() {
        return latestUpdateTime;
    }

    /**
     * @param latestUpdateTime the latestUpdateTime to set
     */
    public void setLatestUpdateTime(Date latestUpdateTime) {
        this.latestUpdateTime = latestUpdateTime;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
}